//
//  DSBuyUnitViewModel.m
//  DSJY
//
//  Created by lx on 17/3/10.
//  Copyright © 2017年 dsjy. All rights reserved.
//  课程购买

#import "DSBuyUnitViewModel.h"
#import "DSAccountViewModel.h"
#import "DSOrderInfo.h"
#import "MTConst.h"
//#import "DDIAPManager.h"

@interface DSBuyUnitViewModel ()
//初始化请求命令
@property (nonatomic, strong, readwrite) RACCommand *initialBindCommand;
//产品
@property(nonatomic, strong, readwrite) NSArray<YQProducts *> *productsList;
@end

@implementation DSBuyUnitViewModel
@synthesize requestCommand = _requestCommand;

#pragma mark - 初始化
- (instancetype)init
{
    if (self = [super init]) {
        //绑定事件
        [self initialBind];
    }
    return self;
}

- (void)initialBind
{
    @weakify(self);
    _initialBindCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id x) {
        @strongify(self);
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [DSNetworkTool getIapProductsWithSuccess:^(NSDictionary *responseObject) {
                //返回值
                self.response = [YQResponse mj_objectWithKeyValues:responseObject];
                [self veriResponse:self.response finishedCallBack:^{
                    //列表
                    self.productsList = [YQProducts mj_objectArrayWithKeyValuesArray:responseObject[@"datas"]];


                    [subscriber sendNext:nil];
                }];
                [subscriber sendCompleted];
            } failure:^(NSError *error) {
                MTLog(@"error:%@", error);
                [SVProgressHUD dismiss];
                [SVProgressHUD showErrorWithStatus:@"系统异常!"];
                [subscriber sendCompleted];
            }];
            
            return nil;
        }];
    }];
    
//    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(DSOrderInfo *orderInfo) {
//        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
//            SKProduct *product = orderInfo.product;
//            [[DDIAPManager sharedInstance] addPayment:product quantity:1 success:^(NSString *receipt, BOOL isReal) {
//                //向服务器发送请求验证，receipt是base64加密过的
//                
//                [DSNetworkTool iapWithParams:[DSAccountViewModel sharedAccount].account year:orderInfo.year channel:orderInfo.channel receipt:receipt chooseEnv:isReal success:^(NSDictionary *responseObject) {
//                    [subscriber sendNext:nil];
//                    MTLog(@"responseObject:%@", responseObject);
//                } failure:^(NSError *error) {
//                    MTLog(@"error:%@", error);
//                    [subscriber sendCompleted];
//                }];
//            } failure:^(NSString *errorMessage, DDIAPManagerFailureType errorType) {
//                MTLog(@"errorMessage:%@--errorType:%ld", errorMessage, (long)errorType);
//                [SVProgressHUD dismiss];
//                [subscriber sendCompleted];
//            }];
//            
//            return nil;
//        }];
//    }];
//    
//    //设置请求时的提示框
//    [self executingWithCommand:_requestCommand];
}

@end
